<?php
    include_once("../inc/login_controll.inc.php");
	include_once("../class/config.class.php"); 
	$config = config::getConfig();
	include_once($config->getValue("home_path")."/inc/defines.inc.php");

	include_once(HOME_PATH."/class/form.class.php");
	include_once(HOME_PATH."/class/database.class.php");
	include_once(HOME_PATH."/class/user.class.php");

	$oForm = new form($config);
	$oDB = new database($config);
	$oForm->setDB($oDB);
	
	$oUser = new user($config);	
	$oUser->setDB($oDB);
	
	if(isset($_POST['save']) && trim($_POST['save'])=="true"){
		// Alle Fragen beantwortet?
		if($oForm->getNumQuestionsForForm($_SESSION['form'])>(count($_POST)-1)){
			$sError = "Bitte beantworten Sie alle Fragen!";
			$_POST['save']=false;
		}else{
			$cnt = 0;
			foreach($_POST as $k => $v){
				if($k!=="save"){
					$k = substr($k,1);
					$aData[] = explode("_",$k);
					$aData[$cnt][] = $v;
					$cnt++;															
				}
			}
			if(!$oForm->saveAnswer($_SESSION['token'], $_SESSION['form'], $aData)){
				$oForm->track("Fehler beim speichern der Antworten! Token = ".$_SESSION['token']);
			}
			header("location:thx.php");
		}
	}
	// Form_id gesetzt?
		if(isset($_SESSION['form']) && trim($_SESSION['form'])!=="" && is_numeric($_SESSION['form'])){
			$aFormDetails = $oForm->getFormDetails($_SESSION['form']);
			// Hier muss noch abgefragt werden ob einer oder mehrere Lehrer für die Umfrage geschaltet sind
			// Wurde erst einmal wieder rausgenommen, eventuell in einer späteren Version wieder implementieren?
/*
			if(isset($aFormDetails['add_teacher']) && trim($aFormDetails['add_teacher']) !==""){
				$aAddTeacher = explode(",",$aFormDetails['add_teacher']);
				$aAddTeacher[] = $aFormDetails['teacher_id'];				
				// Hier noch für jeden einzelnen Lehrer den Namen auslesen!
				foreach($aAddTeacher as $k=>$v){
					$aTeacherDetails[] = $oUser->getTeacherDetail($v);
				}
			}	
*/

			// Tabellen Header
			include(HOME_PATH."/templates/header.tpl.php");
			echo "<div id='content'>";
			include(HOME_PATH."/templates/form_header.tpl.php");
			$aQuestion = $oForm->getQuestionsByFormNorm($_SESSION['form']);		
			$iNumQuestion = count($aQuestion);
		
			//Tabellen Zeilen			
			for($i=0; $i<count($aQuestion); $i++){			
				include(HOME_PATH."/templates/question_row.tpl.php");
			}
			
			//Tabellen Footer
			include(HOME_PATH."/templates/form_footer.tpl.php");
			include(HOME_PATH."/templates/footer.tpl.php");
			echo "</div>";
		}
	
?>